package com.wbxqy.car_management_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wbxqy.car_management_system.dto.TimeSlot;
import com.wbxqy.car_management_system.entity.Car;
import com.wbxqy.car_management_system.service.ICarRequestService;
import com.wbxqy.car_management_system.service.ICarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 车辆表 前端控制器
 * </p>
 *
 * @author wbxqy
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/wbxqy/cars")
public class CarController {

    @Autowired
    private ICarService carService;

    @GetMapping("/page")
    public Page<Car> getCarList(@RequestParam(required = false, defaultValue = "1") Integer page,
                                @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                @RequestParam(required = false) String licensePlate,
                                @RequestParam(required = false) String brand,
                                @RequestParam(required = false) String status) {
        // 构造分页构造器
        final Page<Car> pageInfo = new Page<>(page, pageSize);
        final QueryWrapper<Car> queryWrapper = new QueryWrapper<>();

        // 添加过滤条件
        if (licensePlate != null && !licensePlate.isEmpty()) {
            queryWrapper.like("license_plate_wbxqy", licensePlate);
        }
        if (brand != null && !brand.isEmpty()) {
            queryWrapper.like("brand_wbxqy", brand);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.like("status_wbxqy", status);
        }

        // 执行分页查询
        carService.page(pageInfo, queryWrapper);

        return pageInfo;
    }


    @PostMapping("/add")
    public void addCar(@RequestBody Car car) {
        carService.save(car);
    }

    @DeleteMapping("/delete/{carId}")
    public void deleteCar(@PathVariable("carId") Long carId) {
        carService.removeById(carId);
    }

    @PutMapping()
    public void editCar(@RequestBody Car car) {
        carService.updateById(car);
    }

    @GetMapping("")
    public List<Car> list() {
        return carService.list();
    }

    @Autowired
    private ICarRequestService carRequestService;

    @GetMapping("/is-free")
    public boolean isCarFree(
            @RequestParam int carId,
            @RequestParam String startTime,
            @RequestParam String endTime) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startTime, formatter);
        LocalDateTime end = LocalDateTime.parse(endTime, formatter);

        return carRequestService.isCarFree(carId, start, end);
    }

    @GetMapping("/busy-time-slots")
    public List<TimeSlot> getCarBusyTimeSlots(@RequestParam int carId) {
        return carRequestService.getCarBusyTimeSlots(carId);
    }

    /**
     * 获取在指定时间段内有空的所有车
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/free-cars")
    public List<Car> getFreeDrivers(
            @RequestParam String startTime,
            @RequestParam String endTime) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startTime, formatter);
        LocalDateTime end = LocalDateTime.parse(endTime, formatter);

        return carRequestService.getFreeCars(start, end);
    }
}